$(document).ready(function(){
	
	$('#addQuestion').click(function(){
		var questionHtml = '<input type="hidden" name="choiceCount[]" value="0">';
		questionHtml += '<div><select name="qTypes[]" style="width:100px">';
		questionHtml += '<option value="open">Open</option>';
		questionHtml += '<option value="mc">MC</option>';
		questionHtml += '</select>';
		questionHtml += ' <input type="text" class="question" name="question[]" placeholder="Enter your question here" size="50">';
		questionHtml += ' <button type="button" class="minusQuestion" style="width:30px">-</button></div>';
		questionHtml += '<div class="error"></div>';
		$('<div>'+questionHtml+'</div><br>').appendTo($('#allQuestionBlock'));
	});
	
	$(document).delegate('.minusQuestion','click',function(){
		var removeElement = $(this).parent().parent();
		if($('.question').size() > 1){
			removeElement.next().remove();
			removeElement.remove();
		}
			
	});
	
	$(document).delegate('select','change',function(){
		var hidden = $(this).parent().siblings('input[type=hidden]').eq(0);
		if($(this).val() == 'mc'){
			var questionBlock = $(this).parent().parent();
			var htmlContent='<div class="table"><div class="tr">';
			htmlContent += '<div class="td"><div><div><input type="text" class="choice" name="choice[]" placeholder="Enter content of choice" size="30">';
			htmlContent += ' <button type="button" class="minusChoice" style="width:30px">-</button></div>';
			htmlContent += '<div class="error"></div></div></div>';
			htmlContent += '<div class="td"><button type="button" class="addChoice" style="margin-left: 30px">+</button></div>';
			htmlContent += '</div></div>';
			hidden.val(parseInt(hidden.val())+1);
			$(htmlContent).appendTo(questionBlock);
		}
		else{
			$(this).parent().siblings('div').eq(1).remove();
			hidden.val(0);
		}
			
	});
	
	$(document).delegate('.addChoice','click',function(){
		var choicesBlock = $(this).parent().siblings('div').eq(0);
		var choiceBlock = choicesBlock.children('div').eq(0);
		var htmlContent = '<div><div><input type="text" class="choice" name="choice[]" placeholder="Enter content of choice" size="30">';
			htmlContent += ' <button type="button" class="minusChoice" style="width:30px">-</button></div>';
			htmlContent += '<div class="error"></div></div>';
		$(htmlContent).appendTo(choicesBlock);
		var hidden = choicesBlock.parent().parent().siblings('input[type=hidden]').eq(0);
		hidden.val(parseInt(hidden.val())+1);
	});
	
	$(document).delegate('.minusChoice','click',function(){
		var removeElement = $(this).parent().parent();
		if(removeElement.siblings('div').size() > 0){
			var hidden = removeElement.parent().parent().parent().siblings('input[type=hidden]').eq(0);
			hidden.val(parseInt(hidden.val())-1);
			removeElement.remove();
		}

		
	});
	
	$('#deadlineEnable').click(function(){
		$('#deadlineError').html('');
		$('#deadlineSetting').toggle(this.checked);
		
	});
	
	$('#passwordEnable').click(function(){
		$('#passwordSetting').toggle(this.checked);
	});
	
	$('#groupsEnable').click(function(){
		$('#groupsSetting').toggle(this.checked);
	});
	
	$(document).delegate('.minusGroup','click',function(){
		if($('.group').size() > 1){			
			$(this).parent().remove();
		}
	});
	
	$('#addGroup').click(function(){
		var groupsBlock = $(this).parent().siblings('div').eq(0);
		var groupHtml = groupsBlock.children('div').eq(0).html();	
		$('<div class="group">'+groupHtml+'</div>').appendTo(groupsBlock);
	});
	
	
});

